package com.rrqs.activity.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.litesuits.http.request.content.multi.MultipartBody;
import com.litesuits.http.request.content.multi.StringPart;
import com.rrqs.MessageEvents;
import com.rrqs.activity.MyMapLocation;
import com.rrqs.activity.SygOrdDetailActivity_;
import com.rrqs.activity.common.Common;
import com.rrqs.activity.common.Const;
import com.rrqs.activity.data.OrderNewHelpData;
import com.rrqs.activity.data.ZhuanDanData;
import com.rrqs.net.AsyncHttpClient;
import com.rrqs.net.HttpResponseListener;
import com.rrqs.util.MyApplication;
import com.rrqs.utils.JsonUtils;
import com.rrqs.views.OnChanged;
import com.zadtek.xmqishi.R;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.List;

/**
 * 待取货
 * 
 * @author kywwl
 * 
 */
public class OrderWaitHelpAdapter extends CommonAdapter<OrderNewHelpData> {
	private OnChanged listener;

	public OrderWaitHelpAdapter(Context context, List<OrderNewHelpData> datas,
			int layoutId) {
		super(context, datas, layoutId);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void convert(ViewHolders holder, final OrderNewHelpData t, final int position) {
		ImageView newtask_order_img = holder.getView(R.id.newtask_order_img);
		TextView newtaskmomey = holder.getView(R.id.newtask_money);
		TextView newtasktime = holder.getView(R.id.newtask_time);

		TextView newtask_goods_name = holder.getView(R.id.newtask_goods_name);
		TextView newtask_goods_address = holder
				.getView(R.id.newtask_goods_address);
		TextView help_goods_distance = holder.getView(R.id.help_goods_distance);
		TextView newtask_goods_distance = holder
				.getView(R.id.newtask_goods_distance);
		LinearLayout wait_layout =holder.getView(R.id.wait_layout);
		newtask_goods_name.setText(t.getTitle());
		newtask_goods_address.setText(t.getGet_address());
		LinearLayout newhelp_layout = holder.getView(R.id.newhelp_layout);
		if (t.getGet_address().length() > 0) {
			newtask_goods_address.setVisibility(View.VISIBLE);
		} else {
			newtask_goods_address.setVisibility(View.GONE);
		}
		
		if (t.getType().equals("buy")) {
			newtask_order_img.setImageResource(R.drawable.gou);
		} else if (t.getType().equals("get")) {
			newtask_order_img.setImageResource(R.drawable.qu);
		} else if (t.getType().equals("send")) {
			newtask_order_img.setImageResource(R.drawable.fa);
		}
		
		LinearLayout juli_layout01 =holder.getView(R.id.juli_layout01);
		if (!t.getLocation_get().equals("0")) {
			help_goods_distance.setText(t.getLocation_get() + "Km");
			juli_layout01.setVisibility(View.VISIBLE);
		} else {
			juli_layout01.setVisibility(View.GONE);
		}
		if (!t.getLocation_send().equals("0")) {
			newtask_goods_distance.setText(t.getLocation_send() + "Km");
			wait_layout.setVisibility(View.VISIBLE);
		} else {
			wait_layout.setVisibility(View.GONE);
		}

		TextView newtask_goods_getaddress = holder
				.getView(R.id.newtask_goods_getaddress);
		TextView getgpsaddr = holder.getView(R.id.getgpsaddr);
		newtaskmomey.setText("¥" + t.getPercentage());
		if (!t.getHelp_addtime().equals("") || t.getHelp_addtime() != null) {
			newtasktime.setText(Common.getStrTime(t.getHelp_addtime()));
		}
		newtask_goods_getaddress.setText(t.getSend_address());

		LinearLayout submitthread = holder.getView(R.id.help_submit);
		submitthread.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (t.getHelp_order_id() != null
						&& !t.getHelp_order_id().equals("")) {
					Intent intent = new Intent(mContext,
							SygOrdDetailActivity_.class);
					intent.putExtra("mainid", t.getHelp_order_id());
					intent.putExtra("location", t.getLocation_send());
					intent.putExtra("location_get", t.getLocation_get());
					mContext.startActivity(intent);
				}
			}
		});

		try {
			String address_road = t.getMap_location();
			if (address_road == null || address_road.equals("")
					|| address_road.equals("null")) {
				getgpsaddr.setVisibility(View.GONE);
			} else {
				getgpsaddr.setText(address_road); // 根据经纬度反查地址
				getgpsaddr.setVisibility(View.VISIBLE);
			}
		} catch (Exception e) {
		}
		//底部按钮点击相关
		TextView lianxiguke = holder.getView(R.id.lianxiguke);
		TextView laixishangjia = holder.getView(R.id.laixishangjia);
		TextView ditudaohang = holder.getView(R.id.ditudaohang);
		TextView querenquhuo = holder.getView(R.id.querenquhuo);
		TextView shangbaodaodian = holder.getView(R.id.shangbaodaodian);
		lianxiguke.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				callPhoneNumber(t.getPhone());
			}
		});
		laixishangjia.setVisibility(View.GONE);
		ditudaohang.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Addressclick(t.getLat(),t.getLng());
			}
		});
		if(t.getFan_status().equals("6")){
			querenquhuo.setVisibility(View.GONE);
			shangbaodaodian.setVisibility(View.VISIBLE);
		}else{
			querenquhuo.setVisibility(View.VISIBLE);
			shangbaodaodian.setVisibility(View.GONE);
		}
		querenquhuo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AsyncHttpClient httpClient = new AsyncHttpClient(MyApplication.getInstace());

				SharedPreferences sp = com.rrqs.util.MyApplication.getInstace().getSharedPreferences(Const.PREF_NAME, Context.MODE_PRIVATE);
				String uid = sp.getString("uid", "");

				MultipartBody body = new MultipartBody();
				body.addPart(new StringPart("emp_id", uid));
				body.addPart(new StringPart("maid_id", ""+t.getOrder_id()));
				body.addPart(new StringPart("types", t.getTypes()+""));
				body.addPart(new StringPart("status", "2"));
				body.addPart(new StringPart("ince", "get_rr_help_wait_order_anniu"));
				httpClient.post("http://xmpc.zadtek.com/mobile/Qs/index", body, new HttpResponseListener() {
					@Override
					public void onFailure(Throwable error) {
						super.onFailure(error);

					}

					@SuppressWarnings("unchecked")
					@Override
					public void onSuccess(String status, String result) {
						super.onSuccess(status, result);
						try {
							JSONObject jsonObject = new JSONObject(result);
							Log.i("CYX","报告到店"+jsonObject);
						}catch (Exception e){

						}
						ZhuanDanData resp = JsonUtils.json2Class(result,ZhuanDanData.class);
						if (resp.getStatus() == 1){
							mDatas.remove(position);
							notifyDataSetChanged();
							EventBus.getDefault().post(new MessageEvents().setEventsType("mainshuaxin"));
							Toast.makeText(mContext,resp.getSucess(), Toast.LENGTH_SHORT).show();
						}else{
							Toast.makeText(mContext,resp.getFail(), Toast.LENGTH_SHORT).show();
						}

					}
				});
			}
		});
		shangbaodaodian.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AsyncHttpClient httpClient = new AsyncHttpClient(MyApplication.getInstace());

				SharedPreferences sp = com.rrqs.util.MyApplication.getInstace().getSharedPreferences(Const.PREF_NAME, Context.MODE_PRIVATE);
				String uid = sp.getString("uid", "");

				MultipartBody body = new MultipartBody();
				body.addPart(new StringPart("emp_id", uid));
				body.addPart(new StringPart("maid_id", ""+t.getOrder_id()));
				body.addPart(new StringPart("types", t.getTypes()+""));
				body.addPart(new StringPart("status", "1"));
				body.addPart(new StringPart("ince", "get_rr_help_wait_order_anniu"));
				httpClient.post("http://xmpc.zadtek.com/mobile/Qs/index", body, new HttpResponseListener() {
					@Override
					public void onFailure(Throwable error) {
						super.onFailure(error);

					}

					@SuppressWarnings("unchecked")
					@Override
					public void onSuccess(String status, String result) {
						super.onSuccess(status, result);
						try {
							JSONObject jsonObject = new JSONObject(result);
							Log.i("CYX","报告到店"+jsonObject);
						}catch (Exception e){

						}
						ZhuanDanData resp = JsonUtils.json2Class(result,ZhuanDanData.class);
						if (resp.getStatus() == 1){
							mDatas.get(position).setFan_status("16");
							notifyDataSetChanged();
							EventBus.getDefault().post(new MessageEvents().setEventsType("mainshuaxin"));
							Toast.makeText(mContext, resp.getSucess(), Toast.LENGTH_SHORT).show();
						}else {
							Toast.makeText(mContext, resp.getFail(), Toast.LENGTH_SHORT).show();
						}
					}
				});
			}
		});
	}
	// 电话
	public void callPhoneNumber(String phoneNumber) {
		if (phoneNumber != null && !phoneNumber.equals("")) {
			Intent intent = new Intent(Intent.ACTION_DIAL);
			intent.setData(Uri.parse("tel:" + phoneNumber));
			if (intent.resolveActivity(mContext.getPackageManager()) != null) {
				mContext.startActivity(intent);
			}
		}
	}
	private void Addressclick(String lat,String lng) {
		if (!lat.equals("0.0") && null != lat && !lng.equals("0.0") && null != lng && !lat.equals("null") && !lng.equals("null")) {
			Intent intent = new Intent(mContext, MyMapLocation.class);
			intent.putExtra("lat", lat);
			intent.putExtra("lng", lng);
			mContext.startActivity(intent);
		} else {
			Common.DisplayToast(mContext, "坐标获取失败！", 2);
		}
	}
}
